<template>
  <div class="favorites">
    <NavBar />
    <h1>我的收藏夹</h1>
    <div class="favorites-list">
      <div v-for="cake in favorites" :key="cake.id" class="favorite-item" @click="showCakeDetail(cake)">
        <img :src="cake.image" class="cake-image" alt="Cake Image"/>
        <h2>{{ cake.name }}</h2>
        <p>价格: {{ cake.price }}元</p>
        <div class="button-group">
          <el-button type="primary" @click="showCakeDetail(cake)">查看详情</el-button>
          <el-button type="danger" @click.stop="removeFromFavorites(cake)">移除收藏</el-button>
        </div>
      </div>
    </div>

    <!-- 商品详情模态框 -->
    <el-dialog title="商品详情" :visible.sync="dialogVisible" width="30%">
      <div class="dialog-content">
        <img :src="selectedCake.image" class="detail-image" alt="Cake Image"/>
        <h2>{{ selectedCake.name }}</h2>
        <p>价格: {{ selectedCake.price }}元</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import NavBar from './NavBar.vue';

export default {
  name: 'Favorites',
  components: {
    NavBar
  },
  data() {
    return {
      favorites: JSON.parse(localStorage.getItem('favorites')) || [],
      dialogVisible: false,
      selectedCake: {}
    };
  },
  methods: {
    removeFromFavorites(cake) {
      const index = this.favorites.findIndex(fav => fav.id === cake.id);
      if (index > -1) {
        this.favorites.splice(index, 1);
        localStorage.setItem('favorites', JSON.stringify(this.favorites));
      }
    },
    showCakeDetail(cake) {
      this.selectedCake = cake;
      this.dialogVisible = true;
    }
  }
};
</script>
<style scoped>
.favorites h1 {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #333;
}

.favorites-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  padding: 20px;
}

.favorite-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 15px;
  width: 250px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  cursor: pointer;
}

.favorite-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.cake-image {
  width: 150px;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 10px;
}

.favorite-item h2 {
  font-size: 18px;
  margin-bottom: 5px;
  color: #333;
}

.favorite-item p {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.button-group {
  display: flex;
  gap: 10px;
}

.button-group .el-button {
  width: 100%;
  margin-top: 0;
}

.dialog-content {
  text-align: center;
}

.detail-image {
  width: 150px;
  height: 150px;
  object-fit: cover;
  margin-bottom: 10px;
  border-radius: 8px;
}

.dialog-footer .el-button {
  width: 100%;
}
</style>